<?php

namespace App\Http\Controllers;

use App\Models\Fine;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Yansongda\Pay\Pay;
use Illuminate\Http\Request;

class PayController extends Controller
{
    protected $config = [
            'app_id' => '2016092200570088',
            'notify_url' => 'http://www.inf.ink./api/alipay/notifiy',
            'return_url' => 'http://www.inf.ink./api/alipay/return',
            'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvpVeINnAeneoxw7YJHqrWzOmOPhEif4zDGLetW+Dh5YKkrS9l0T1pFRwX5uVCXoliz+oo8zfQX5K2zCiu9iX6la5qLw37zefaVjXTxndtcfsQN58LLITjJz30n6sLrIBiYf1u4S2j8/eZetNAjW+5SJO2UVPaWSO5XP7LuQO1AjAKk39+IURGVWHHzmesRl+qLF7+ZqZQzKcaZisDll/uHCipLo9uNFuX75LOoaCrW5C3LahuJ8lWXVUm65a939ihl1/N0mn6Ak5B26p4ELcCk16+kFU/8GdgXTez4YndyqlpCS16mh9MqrTnHMa+buoV29lS1DtUI4O6mrCQVd7uQIDAQAB',
            'private_key' => 'MIIEpAIBAAKCAQEAyWQEaay864zXey7DbLnx0MWl9y+uUwacGa7O+CGR+eJPrZXYtFrASPEzIAVgkGcvKYOaUzzVQic9WOndWQNC/p7moVcQGQKz7jW3Rnu6Zko4ibOqvXXFMcAWDQWOVlysvvZ838mPMQUl7egDqOggK4KNpx5ORTy98xsvcLma4rNO/7AqZLlsE+cUYcbSmplG0XFJiAIUBNElHXDLeB1s8Or5By+Eda8cmwTlwJkTZzX4fYEKzcot5JBuuHEEaoVz+gvcK4XqVNpCHqORvFhl9tfFDZZ7StQhwOQBvSlAyFahK+RuM7PrQUsaAY7W+R0lkIIBunUq05iVrgGJPBZK4wIDAQABAoIBAQC6Zdo3vOu20Mr4dpXmYkxQwT8VhIpCylFc7BEgVGH5+N9qeND0TTK/0I9TSebQgNCECgyikiGIcy0yyHJY2s39QTGRjzWVEI1dI1aY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0T4rw9bMN2Ksf+XoNc+FsvnK/EJMDtKtU8vpWaaO5GA=='
    ];

    public function index(Request $request)
    {
        $validate=Validator::make($request->all(),[
            'price'=>'numeric|required',
        ]);

        if($validate->fails())
        {
            $error=$validate->errors();
            return $this->fail(400006,$error);
        }

        $price=$request->get('price');

        $config_biz = [
            'out_trade_no' => time(),
            'total_amount' => $price,
            'subject'      => '图书借阅超时罚款'
        ];

        return Pay::alipay($this->config)->web($config_biz);
    }

    public function getReturn(Request $request)
    {
        return redirect("/library/server/index.html??%E6%AF%9B%E6%AF%9B&eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6Ly8xMjAuNzkuMTgzLjE0Mi9hcGkvbG9naW4iLCJpYXQiOjE1MzczODAzOTgsImV4cCI6MTUzNzQ2Njc5OCwibmJmIjoxNTM3MzgwMzk4LCJqdGkiOiJSUGpEZDByVTVZY0tITnNwIn0.RYf5JZbhvxuXRqLkid9h4W5WeYbRe5wqjzlkRVyyka4");
    }

    public function notify(Request $request)
    {
        if (Pay::alipay($this->config)->verify($request->all())) {
            file_put_contents(storage_path('notify.txt'), "收到来自支付宝的异步通知\r\n", FILE_APPEND);
            file_put_contents(storage_path('notify.txt'), '订单号：' . $request->out_trade_no . "\r\n", FILE_APPEND);
            file_put_contents(storage_path('notify.txt'), '订单金额：' . $request->total_amount . "\r\n\r\n", FILE_APPEND);
        } else {
            file_put_contents(storage_path('notify.txt'), "收到异步通知\r\n", FILE_APPEND);
        }

        echo "success";
    }
}